const app = getApp();
Page({
  data: {
    smsAvailable: true,
    smsSeconds: 60,
    disabled: false,
    phoneNo:'',
    code:'',
  },
  whenInput: function (e) {
    var v = e.detail.value;
    var nodeType = e.currentTarget.dataset.nodetype;
   if (nodeType == 'phoneNo') {
      this.data.phoneNo= v
    } else if (nodeType == 'vaCode') {
      this.data.code = v;
    }
  },
  doSave: function (e) {
    var _this = this;
    app.doReq(app.globalData.SERVICE.USERCENTER.PHONE_BIND, {
      phone: _this.data.phoneNo,
      code: _this.data.code,
      openId: wx.getStorageSync("openid"),
    }, 'get',
      function (res) {
        var data = res.data;
        if (data.code == 200 && data.type == 'SUCCESS') {
          wx.showToast({
            title: '绑定成功',
            icon: 'success'
          });
          var pages = getCurrentPages();
          var currentPage = pages[pages.length - 1];//当前页面
          var pervPagge = pages[pages.length - 2];//上个页面
          pervPagge.setData({
            addName: e.currentTarget.dataset.city + e.currentTarget.dataset.country,
          })
          wx.navigateBack({});
        } else {
          wx.showToast({
            title: data.errmsg,
            icon: 'none'
          })
        }
      });
   
  },
  getVaCode: function () {
    var _this = this;
    var phoneNo = this.data.phoneNo
    if (this.data.smsAvailable) {
      this.setData({
        smsSeconds: 60,
        disabled: false,
      });
      if (phoneNo == undefined || phoneNo == null || phoneNo.trim() === '') {
        wx.showToast({
          title: '请输入手机号！',
          icon: 'none'
        })
        return
      } else {

        app.doReq(app.globalData.SERVICE.USERCENTER.SEND_CAPTCHA, { phone: phoneNo }, 'get', function (res) {
          if (res.data.code == 200) {
              wx.showToast({
                title: '发送成功',
                icon:'success'
              })
          }
        });
        _this.setData({
          smsAvailable: false,
          disabled: true
        })
        var timer = setInterval(function () {
          if (_this.data.smsSeconds == 1) {
            _this.setData({
              smsAvailable: true,
              disabled: false
            })
            clearInterval(timer);
          } else {
            var sec = _this.data.smsSeconds;
            sec--
            _this.setData({
              smsSeconds: sec
            })
          }
        }, 1000);
      }
    }
  }
});
